package com.dragon.modules.yuegou.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.system.DictEntity;
import com.dragon.modules.yuegou.enums.system.DictStatus;
import com.dragon.modules.yuegou.mapper.system.DictMapper;
import com.dragon.modules.yuegou.model.system.Dict;
import com.dragon.modules.yuegou.query.system.DictQuery;
import com.dragon.modules.yuegou.service.system.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-19 08:59:41
 * @version 2.0
 */
@Service("sysDictService")
public class DictServiceImpl
		extends ServiceImpl<DictMapper, DictEntity>
		implements DictService {

	@Override
	public IPage<Dict> search(PageRequest page, DictQuery query) {
		query.setDel(false);
		QueryWrapper<DictEntity> qw = query.toQueryWrapper();
		qw.orderByAsc(Dict.COL_SORT_NO);
		qw.orderByDesc(Dict.COL_CREATED_TIME);

		IPage<DictEntity> pageEntity = new Page<>(page.getPage(), page.getSize(), page.getCount());
		IPage<DictEntity> pageEntites = baseMapper.selectPage(pageEntity, query.toQueryWrapper());
		return pageEntites.convert(Dict::new);
	}

	@Override
	public void saveDict(Dict dict) {
		if (dict.getSortNo()==null) {
			dict.setSortNo(1);
		}
		dict.setStatus(DictStatus.ENABLED);
		save(dict);
	}

	@Override
	public void deleteDic(String dicId) {
		DictEntity dict =new DictEntity();
		dict.setDel(true);
		dict.setId(dicId);
		baseMapper.updateById(dict);
	}

	@Override
	public List<Dict> dictList() {
		DictQuery dictQuery = new DictQuery();
		dictQuery.setType("bank_code");
		dictQuery.setDel(false);
		dictQuery.setStatus(DictStatus.ENABLED);
		return list(dictQuery);
	}

	@Override
	public Dict getDict(String code, String type) {
		if (StringUtils.isEmpty(code)){
			return null;
		}
		DictQuery dictQuery = new DictQuery();
		dictQuery.setType(type);
		dictQuery.setCode(code);
		dictQuery.setDel(false);
		dictQuery.setStatus(DictStatus.ENABLED);

		List<DictEntity> dictEntities = baseMapper.selectList(dictQuery.toQueryWrapper());
		if (CollectionUtils.isEmpty(dictEntities)){
			return null;
		}
		return new Dict(dictEntities.get(0));
	}

	@Override
	public List<Dict> getDictList(String type) {
		DictQuery dictQuery = new DictQuery();
		dictQuery.setType(type);
		dictQuery.setDel(false);
		dictQuery.setStatus(DictStatus.ENABLED);
		return list(dictQuery);
	}

	private List<Dict> list(DictQuery dictQuery){
		QueryWrapper<DictEntity> qw = dictQuery.toQueryWrapper();
		qw.orderByAsc(Dict.COL_SORT_NO);
		List<DictEntity> entities = baseMapper.selectList(qw);
		return entities.stream().map(Dict::new).collect(Collectors.toList());
	}

}